package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.page.PageBean;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TypeTempService;
import com.dongdongshop.vo.SpecificationVO;
import com.dongdongshop.vo.TemplateVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class TypeTempServiceImpl implements TypeTempService {

    @Resource
    private TbTypeTemplateMapper tm;

    @Resource
    private TbItemCatMapper cm;

    @Resource
    private TbSpecificationOptionMapper to;

    @Override
    public PageBean<TbTypeTemplate> getAll(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbTypeTemplate> templates = tm.selectByName(name);
        PageInfo<TbTypeTemplate> pageInfo = new PageInfo<>(templates);
        return new PageBean<>(pageNum,pageSize,pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getList());
    }



    @Override
    public int insert(TbTypeTemplate t) {
        return tm.insertSelective(t);
    }

    @Override
    public TbTypeTemplate getById(Long id) {
        return tm.selectByPrimaryKey(id);
    }

    @Override
    public int update(TbTypeTemplate t) {
        return tm.updateByPrimaryKeySelective(t);
    }

    @Override
    public int delete(Long id) {
        return tm.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteCheck(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
        return 1;
    }

    @Override
    public TemplateVO getTypeTemplates(Long itemCatId) {
        //根据分类id查询分类对象
        TbItemCat tbItemCat = cm.selectByPrimaryKey(itemCatId);
        //取出模板id
        Long typeId = tbItemCat.getTypeId();
        //根据模板id查询模板对象
        TbTypeTemplate tbTypeTemplate = tm.selectByPrimaryKey(typeId);
        TemplateVO vo = new TemplateVO();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        //从模板中取出规格的字符串
        String specIds = tbTypeTemplate.getSpecIds();
        List<SpecificationVO> specificationVOS = JSONObject.parseArray(specIds, SpecificationVO.class);
        //根据vo的id查看options
        for (SpecificationVO specificationVO : specificationVOS) {
            Long id = specificationVO.getId();
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            example.createCriteria().andSpecIdEqualTo(id);
            List<TbSpecificationOption> options = to.selectByExample(example);
            //放到vo类中
            specificationVO.setOptionList(options);
        }
        //最后将规格的list放到template中
        vo.setSpecificationVOList(specificationVOS);
        return vo;
    }
}
